﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class CMS_Admin_AddBlock : System.Web.UI.Page
{
    Usas.BLL.Cms.cms_block bl = new Usas.BLL.Cms.cms_block();
    Usas.BLL.Cms.cms_block_setting cbs = new Usas.BLL.Cms.cms_block_setting();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            BindDDL();
        }
    }

    private void BindDDL()
    {
        string page = Request["page"].ToString();
        DataSet ds = bl.GetList("b_id not in(select s_b_id from cms_block_setting where s_page ='" + page + "')");

        if (ds != null && ds.Tables[0] != null)
        {
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                ddl_current_list.Items.Add(new ListItem(ds.Tables[0].Rows[i]["b_name"].ToString(), ds.Tables[0].Rows[i]["b_id"].ToString()));
            }
        }
    }

    protected void add_Click(object sender, EventArgs e)
    {
        if (ddl_current_list.SelectedValue == "")
        {
            return;
        }
        string page = Request["page"].ToString();
        string place = Request["block"].ToString();
        string num = "";
        DataSet ds = cbs.GetList("s_location in (select max(s_location) from cms_block_setting where s_location like '" + place + ",%')");

        if (ds == null || ds.Tables[0].Rows.Count <= 0)
        {
            num = place + ",0";
        }
        else
        {
            num = place + "," + (Convert.ToInt32((ds.Tables[0].Rows[0]["s_location"].ToString().Split(','))[1]) + 1);
        }
        Usas.Model.Cms.cms_block_setting cbsm = new Usas.Model.Cms.cms_block_setting();
        cbsm.s_page = page;
        cbsm.s_b_id = Convert.ToInt32(ddl_current_list.SelectedValue);
        cbsm.s_location = num;

        if (cbs.Add(cbsm) > 0)
        {
            ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('添加成功！');parent.location.reload()", true);
        }
    }
}
